<?php

use Illuminate\Database\Seeder;
use App\User;
use App\Product;

class ProductsTableSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        // 获取用户的id
        $user_ids = User::all()->pluck('id')->toArray();

    	$faker = app(Faker\Generator::class);

        $products = factory(Product::class)
                    ->times(50)
                    ->make()
                    ->each(function ($product, $index)
                        use ($user_ids, $faker)
                        {
                            $product->user_id = $faker->randomElement($user_ids);
                        });

        Product::insert($products->toArray());

    }
}
